<template>
	<view class="box">
		<view class="logo">
			<image src="../../static/loginLogo.png" mode=""></image>
		</view>
		<view class="btnChat">
			<button type="default" @tap="checkOpenGps">授权定位</button>
			<view class="desc">授权定位代表您已同意
				<text style="color: #06deff;">《用户协议》</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				avatarUrl: '',
				nickName: '请登录',
				sessionKey: '',
				loginbtnshow: true,
				api: '',
				imgSrc: "../../static/images/mine/icon_next.png"
			}
		},
		onLoad() {
			this.api = this.$reqConfig.UPLOADAPI
			uni.showToast({
				title: '为了使我们更好的为您提供服务，请允许授权我们获取您的位置',
				icon: "none",
				duration:4000
			})
		},
		methods: {
			// 判断是否开启gps
			checkOpenGps() {
				var that = this;
				wx.getSystemInfo({
					success(res) {
						var isopendingwei = res.locationEnabled;
						if (isopendingwei == false) {
							uni.showToast({
								title: '请先开启手机GPS定位',
								icon: "none"
							})
							return
						}

						that.getLocation()
					}
				})
			},

			// 获取定位
			getLocation() {
				let that = this
				uni.getSetting({
					success(res) {
						console.log('1',res);
						// 未授权
						uni.authorize({
							scope: 'scope.userLocation',
							success() { //1.1 允许授权
								wx.getSystemInfo({
									success(res) {
										that.isopendingwei = res.locationEnabled;
										if (that.isopendingwei == false) {
											uni.showToast({
												title:'请先开启手机GPS定位,然后重新刷新',
												icon:"none",
												duration:3000,
											})
											return
										}
									}
								})
								if(!that.isopendingwei){
									uni.showToast({
										title:'请先开启手机GPS定位,然后重新刷新',
										icon:"none",
										duration:3000,
									})
									return
								}
							
								uni.showLoading({
									title: "加载中"
								})
								that.getLocation2()
							},
							fail() { //1.2 拒绝授权
								uni.showModal({
									content: '检测到您没打开获取信息功能权限，是否去设置打开？',
									confirmText: "确认",
									cancelText: '取消',
									success: (res) => {
										if (res.confirm) {
											uni.openSetting({
												success: (res) => {
													console.log('确定');
													that.getLocation2()
												}
											})
										} else {
											console.log('取消');
											return false;
										}
									}
								})
							}
						})

					},

				});
			},
			
			getLocation2(){
							uni.getLocation({
								type: 'wgs84',
								geocode: true,
								success: function(res) {
									console.log('2',res);
							
									uni.setStorage({
										key: 'dingwei',
										data: {
											longitude: res.longitude,
											latitude: res.latitude
										},
									});
									uni.switchTab({
										url:"./growVegetables"
									})
								}
							});
						},

		}
	}
</script>

<style scoped>
	.box {
		display: flex;
		flex-direction: column;
		justify-content: space-around;
		align-items: center;
		height: 100vh;
		background-image: linear-gradient(#b5d341, #5eae35);
	}

	.back image {
		height: 100%;
	}

	.logo {
		width: 300rpx;
		height: 300rpx;
	}

	.logo image {
		width: 100%;
		height: 100%;
	}

	.btnChat {
		margin-bottom: 190rpx;
	}

	.btnChat button {
		width: 530rpx;
		margin-bottom: 20rpx;
		border-radius: 100rpx;
		color: #7dd44d;
	}

	.desc {
		font-size: 30rpx;
		color: #fff;
		text-align: center;
	}
</style>
